<?php

class Admin_m extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    function _check_valid_login($email, $password) {
        $query = $this->db->select("email, password")
                ->from("users")
                ->where("email", $email)
                ->where("password", $password)
                ->get();
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    function _get_user_infor($email) {
        $query = $this->db->select()
                ->from("users")
                ->where("email", $email)
                ->limit(1)
                ->get();
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--get user role--//
    //--get users--//
    public function _get_users($perpage, $offset) {
        $query = $this->db->select("users.*, role.role_name")
                ->from("users")
                ->join("role", "users.id_role = role.id")
                ->limit($perpage, $offset)
                ->get();
        if (count($query->result_array()) > 0) {
            return $query->result_array();
        } else {
            return array();
        }
    }

    //--count users--//
    public function _count_users() {
        $query = $this->db->select("users.*, role.role_name")
                ->from("users")
                ->join("role", "users.id_role = role.id")
                ->get();
        if ($query) {
            $count = $query->num_rows();
            return $count;
        } else {
            return FALSE;
        }
    }

    //--get field by input--//
    function _get_field_input($table, $field, $input) {
        $query = $this->db->select()
                ->from($table)
                ->where($field, $input)
                ->get();
        if ($query->num_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //--add data to table user--//
    function add_user($addition_data) {
        $data = array();
        $collums = array("ho_ten", "password", "email", "id_role");
        foreach ($collums as $field) {
            if (is_array($addition_data) && isset($addition_data[$field])) {
                $data[$field] = $addition_data[$field];
            } elseif ($this->input->post()) {
                $data[$field] = $this->input->post($field);
            }
        }

        $query = $this->db->insert("users", $data);
        if ($query) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

//    //--add data to table user profile--//
//    function add_user_profile($addition_data) {
//        $data = array();
//        $collums = array("user_id", "first_name", "last_name");
//        foreach ($collums as $field) {
//            if (is_array($addition_data) && isset($addition_data[$field])) {
//                $data[$field] = $addition_data[$field];
//            } elseif ($this->input->post()) {
//                $data[$field] = $this->input->post($field);
//            }
//        }
//
//        $query = $this->db->insert("user_profile", $data);
//        if ($query) {
//            return TRUE;
//        } else {
//            return FALSE;
//        }
//    }
//    //--get insert id--//
//    function _get_insert_id() {
//        if ($this->db->insert_id()) {
//            return $this->db->insert_id();
//        } else {
//            return NULL;
//        }
//    }
    //--Get user profile--//
    function get_user_profile($user_id = null) {
        $query = $this->db->select("user_profile.*, users.user_name, users.email")
                ->from("user_profile")
                ->join("users", "user_profile.user_id = users.id", "left")
                ->where("user_profile.user_id", $user_id)
                ->limit(1)
                ->get();
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return array();
        }
    }

    //--get user_id--//
    function _get_user_id($id) {
        $query = $this->db->select("users.id")
                ->from("users")
                ->where("users.id", $id)
                ->get();
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--edit data of table user--//
    function edit_user($addition_data, $id) {
        $data = array();
        $collums = array("first_name", "last_name", "DOB", "telephone", "mobilephone", "address", "company", "gender");
        foreach ($collums as $field) {
            if (is_array($addition_data) && isset($addition_data[$field])) {
                $data[$field] = $addition_data[$field];
            } elseif ($this->input->post()) {
                $data[$field] = $this->input->post($field);
            }
        }

        $query = $this->db->update("user_profile", $data, array("user_id" => $id));
        if ($query) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    //--update last login time--//
    function update_last_login($data, $user_id) {
        $query = $this->db->update("users", $data, array("id" => $user_id));
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--check current active status--//
    function _check_active_status($id) {
        $query = $this->db->select("users.active")
                ->from("users")
                ->where("users.id", $id)
                ->limit(1)
                ->get();
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--change active status--//
    function update_active_status($data, $id) {
        $query = $this->db->update("users", $data, array("id" => $id));
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--get old password--//
    function get_old_password($password, $id) {
        $query = $this->db->select("users.password")
                ->from("users")
                ->where("users.password", $password)
                ->where("users.id", $id)
                ->limit(1)
                ->get();
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--change password--//
    function change_password($data, $id) {
        $query = $this->db->update("users", $data, array("id" => $id));
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    //--delete user--//
    function delete_user($id) {
        $query = $this->db->delete("users", array("id" => $id));
        return $this->db->affected_rows() ? true : false;
    }

    //--delete user profile--//
    function delete_user_profile($id) {
        $query = $this->db->delete("user_profile", array("user_id" => $id));
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    }

    function get_user_role($email) {
        $query = $this->db->select("users.*, role.id as roleid, role.name as role_name")
                ->from("users")
                ->join("role", "role.id = users.role_id")
                ->where("email", $email)
                ->limit(1)
                ->get();
        if ($query) {
            return $query->row();
        } else {
            return FALSE;
        }
    }

}